package nc.tb.pub.tree;

import java.util.HashMap;

import nc.tb.dim.DimDef;
import nc.tb.dim.DimMember;
import nc.tb.pub.BusiObj;
import nc.tb.pub.IBusiObj;
import nc.tb.pub.IDimPkConst;
import nc.tb.pub.IDimType;

public class TbExtraTreeModel extends TbFCTreeModel {

	public TbExtraTreeModel(IBusiObj rootvo, IBusiObj[] vos) {
		super(rootvo, vos);
	}
	public TbExtraTreeModel(IBusiObj rootvo, IBusiObj[] vos, boolean isNoCheckRoot) {
		super(rootvo, vos, isNoCheckRoot);
	}
	public TbExtraTreeModel(TbTreeNode root, IBusiObj[] vos) {
		super(root, vos);
	}
	public TbExtraTreeModel(TbTreeNode root, IBusiObj[] vos, boolean isNoCheckRoot) {
		super(root, vos, isNoCheckRoot);
	}
	public TbExtraTreeModel(TbTreeNode root, IBusiObj[] vos, HashMap<IBusiObj, IBusiObj> hmChild2Parent) {
		super(root, vos, hmChild2Parent);
	}
	
	protected static String getKeyByBusiObj(IBusiObj bo) {
		if (bo != null && bo.getPrimaryKey() != null) {
			StringBuffer key = new StringBuffer();
			String par = getPkParent(bo.getBusiObj());
			if (par != null)
				key.append(par);
			else
				key.append("null");
			key.append(getPrimaryKey(bo.getBusiObj())/*bo.getPrimaryKey()*/);
			return key.toString();	
		}
		else
			return null;
	}
	protected static String getUplevelKeyByBusiObj(IBusiObj bo) {
		if (bo != null && bo.getBusiObj() != null && bo.getBusiObj().getPk_uplevel() != null) {
			StringBuffer key = new StringBuffer();
			String par = getPkParent(bo.getBusiObj());
			if (par != null)
				key.append(par);
			else
				key.append("null");
			key.append(bo.getBusiObj().getPk_uplevel());
			return key.toString();	
		}
		else
			return null;
	}
	protected static String getPkParent(BusiObj bo) {
		if (bo == null)
			return null;
		if (bo instanceof DimMember) {
			try {
				return getPrimaryKey(((DimMember)bo).getDimendef())/*((DimMember)bo).getDimendef().getPrimaryKey()*/;
			} catch (Exception e) {
				return bo.getPk_parent();
			}
		}
		else
			return bo.getPk_parent();
	}
	protected static String getPrimaryKey(BusiObj bo) {
		if (bo != null && bo instanceof DimDef) {
			DimDef dd = (DimDef)bo;
//			if (IDimType.ENTITY.equals(dd.getType()))
//				return "tb"+bo.getPrimaryKey();
			if (IDimPkConst.DEF_ENTITY.equals(dd.getPrimaryKey()) && IDimType.UAPDEF.equals(dd.getType()))
				return "uap"+bo.getPrimaryKey();
		}
		return bo==null ? null : bo.getPrimaryKey();
	}
	protected TbFCTreeModel getNewCloneInstance(TbTreeNode cloneRoot) {
		return new TbExtraTreeModel(cloneRoot, null);
	}
}
